// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
//
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.ui.migration;

import java.util.List;

import org.dengues.commons.IDenguesSharedImage;
import org.dengues.commons.utils.ImageUtil;
import org.dengues.core.migration.AbstractMigrationService;
import org.dengues.ui.i18n.Messages;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-5-8 qiang.zhang $
 * 
 */
public class MigrationSummaryDialog extends TitleAreaDialog {

    private static final Color BCK_COLOR = new Color(null, 255, 255, 255);

    private static final int HORIZONTAL_MERGE = 5;

    private static final int VERTICAL_MERGE = 5;

    private static final int HORIZONTAL_SPACE = 5;

    private static final int VERTICAL_SPACE = 5;

    private static final int DIALOG_HEIGHT = 350;

    private final List<AbstractMigrationService> tasks;

    /**
     * Qiang.Zhang.Adolf@gmail.com MigrationSummaryDialog constructor comment.
     * 
     * @param parentShell
     */
    public MigrationSummaryDialog(Shell parentShell, List<AbstractMigrationService> tasks) {
        super(parentShell);
        setHelpAvailable(false);
        this.tasks = tasks;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
     */
    @Override
    protected void createButtonsForButtonBar(Composite parent) {
        createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.dialogs.TitleAreaDialog#createDialogArea(org.eclipse.swt.widgets.Composite)
     */
    @Override
    protected Control createDialogArea(Composite parent) {
        int height = DIALOG_HEIGHT - getTitleImageLabel().getSize().y;
        Composite dial = new Composite(parent, SWT.NONE);

        parent.setBackground(BCK_COLOR);
        dial.setLayout(new FillLayout());
        dial.setLayoutData(new GridData(GridData.FILL_BOTH));
        dial.setBackground(BCK_COLOR);

        FormData data;

        ScrolledComposite scrolled = new ScrolledComposite(dial, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
        scrolled.setLayout(new GridLayout());
        scrolled.setExpandHorizontal(true);
        scrolled.setBackground(BCK_COLOR);

        Composite container = new Composite(scrolled, SWT.NONE);
        scrolled.setContent(container);
        FormLayout layout2 = new FormLayout();
        container.setLayout(layout2);
        container.setBackground(BCK_COLOR);

        Control lastControl = null;

        int i = 0;
        for (AbstractMigrationService task : tasks) {
            Label imageLabel = new Label(container, SWT.NONE);
            imageLabel.setBackground(BCK_COLOR);
            imageLabel.setImage(task.getImage());
            data = new FormData();
            data.left = new FormAttachment(HORIZONTAL_MERGE);
            if (lastControl == null) {
                data.top = new FormAttachment(VERTICAL_MERGE);
            } else {
                data.top = new FormAttachment(lastControl, VERTICAL_SPACE, SWT.BOTTOM);
            }
            imageLabel.setLayoutData(data);

            Label taskNameLabel = new Label(container, SWT.NONE);
            taskNameLabel.setText(task.getName());
            taskNameLabel.setFont(JFaceResources.getFontRegistry().getBold(JFaceResources.DEFAULT_FONT));
            taskNameLabel.setBackground(BCK_COLOR);
            data = new FormData();
            data.left = new FormAttachment(imageLabel, HORIZONTAL_SPACE);
            data.top = new FormAttachment(imageLabel, 0, SWT.CENTER);
            taskNameLabel.setLayoutData(data);

            Label taskDescLabel = new Label(container, SWT.WRAP);
            taskDescLabel.setText(task.getDescription());
            taskDescLabel.setBackground(BCK_COLOR);
            data = new FormData();
            data.left = new FormAttachment(HORIZONTAL_MERGE);
            data.right = new FormAttachment(100, -HORIZONTAL_MERGE);
            data.top = new FormAttachment(taskNameLabel, VERTICAL_SPACE, SWT.BOTTOM);
            taskDescLabel.setLayoutData(data);
            lastControl = taskDescLabel;
            if (i + 1 < tasks.size()) {
                Label separator = new Label(container, SWT.SEPARATOR | SWT.HORIZONTAL);
                data = new FormData();
                data.left = new FormAttachment(HORIZONTAL_SPACE);
                data.right = new FormAttachment(100, -HORIZONTAL_SPACE);
                data.top = new FormAttachment(taskDescLabel, HORIZONTAL_SPACE);
                separator.setLayoutData(data);
                lastControl = separator;
            }
            i++;
        }
        setTitleImage(ImageUtil.getImage(IDenguesSharedImage.WIZARD_MIGRATION));
        setTitle(Messages.getString("MigrationSummaryDialog.title")); //$NON-NLS-1$
        container.setSize(container.computeSize(height, SWT.DEFAULT));
        return dial;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
     */
    @Override
    protected void configureShell(Shell newShell) {
        super.configureShell(newShell);
        newShell.setSize(600, DIALOG_HEIGHT);
        newShell.setText(Messages.getString("MigrationSummaryDialog.title")); //$NON-NLS-1$
    }

}
